package com.kidback.bbloc259.wxapi;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.kidback.bbloc259.app.KidGlobal;
import com.kidback.bbloc259.util.ToastUtils;
import com.mylhyl.circledialog.BaseCircleDialog;
import com.mylhyl.circledialog.CircleDialog;
import com.mylhyl.circledialog.params.ProgressParams;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelpay.PayResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
//import com.zw.game.app.provider.AppDataProvider;
//import com.zw.game.gameEx.dialog.GameSNSHelper;
//import com.zw.game.utils.TipUtils;
//import com.zw.game.utils.ToastUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.disposables.Disposable;

/**
 * Created by meikai on 2020/07/05.
 */
public class WXPayEntryActivity extends AppCompatActivity implements IWXAPIEventHandler {

    // IWXAPI 是第三方app和微信通信的openApi接口
    private IWXAPI api;

    public WXPayEntryActivity activity;
    public List<Disposable> disposableList = new ArrayList<>();
    private BaseCircleDialog loadingTipDialog;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        activity = this;
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);//取消标题
        setContentView(new View(this));

        // 通过WXAPIFactory工厂，获取IWXAPI的实例
        api = WXAPIFactory.createWXAPI(this, KidGlobal.WX_APP_ID, true);
        api.handleIntent(getIntent(), this);
    }

    @Override
    public void finish() {
        super.finish();
        Log.e("mk", "finish");
        overridePendingTransition(0, 0);
    }

    @Override
    public void onReq(BaseReq baseReq) {

    }

    @Override
    public void onResp(BaseResp baseResp) {
        if (baseResp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
            if (baseResp.errCode == BaseResp.ErrCode.ERR_OK) {
                //支付成功
                ToastUtils.show("支付成功");
                String prepareId = ((PayResp) baseResp).prepayId;

                Map<String, String> paramsMap = new HashMap<>();
                paramsMap.put("prepareId", prepareId);

                paySuccess(paramsMap);

            } else {
                //支付失败
                ToastUtils.show("支付未完成");
                finish();
            }
        }
    }

    private void paySuccess(Map<String, String> paramsMap) {
//        TipUtils.showSuccess("支付成功");
//        AppDataProvider.getInst().requestRefresh();

        finish();
    }

    public void showLoading(String msg) {
        hideLoading();

        loadingTipDialog = new CircleDialog.Builder()
                //添加标题，参考普通对话框
                .setProgressStyle(ProgressParams.STYLE_SPINNER)//STYLE_HORIZONTAL 或 STYLE_SPINNER
                .setProgressText(msg)// style = 水平样式时，text 支持 String.format() 例如：已经下载%s
                //添加按钮，参考普通对话框
                .show(getSupportFragmentManager());
    }

    public void hideLoading() {
        if (loadingTipDialog != null && !isDestroyed()) {
            loadingTipDialog.dismiss();
        }
    }

}
